import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class ButtonDialog extends JDialog implements ActionListener {

	//Declare Swing components
	private JButton buttonSave, buttonExit;
	private JLabel labelID, labelName, labelCourse, labelYear, labelSchedule, labelClassCode;
	protected static JTextField txtID, txtName, txtCourse, txtYear, txtSchedule, txtClassCode;
	private boolean edit = false;
	
	int keyCode;

	//Constructor
	ButtonDialog(JFrame frameParent, String title){

		//Initialize JButton components
		buttonSave = new JButton("Save");
		buttonSave.addActionListener(this);
		buttonExit = new JButton("Exit");
		buttonExit.addActionListener(this);

		//Initialize JLabel components
		labelID = new JLabel("ID Number : ");
		labelName = new JLabel("Full Name: ");
		labelCourse = new JLabel("Course: ");
		labelYear = new JLabel("Year: ");
		labelSchedule = new JLabel("Schedule: ");
		labelClassCode = new JLabel("Class Code: ");

		//Initialize JTextField components
		txtID = new JTextField(15);
		txtName = new JTextField(25);
		txtCourse = new JTextField(15);
		txtYear = new JTextField(15);
		txtSchedule = new JTextField(15);
		txtClassCode = new JTextField(15);
		
		txtID.addKeyListener(keyListener);
		txtYear.addKeyListener(keyListener);
		txtYear.setDocument(new JTextFieldLimit(1));

		//Checks the title if it is equivalent to "EDIT"
		if(title == "EDIT"){
			edit = true;
			StudentMainClass.setFields(StudentGUI.tableStudList.getSelectedRow());
		}

		//Declare JPanel for JLabels and JTextFields with GridBagLayout and GridBagConstraints
		JPanel fieldPanel = new JPanel(new GridBagLayout());
		GridBagConstraints d = new GridBagConstraints();

		d.fill = GridBagConstraints.HORIZONTAL;
		d.weightx = 0.6;
		d.weighty = 0.6;
		d.insets = new Insets(3,3,3,3);
		fieldPanel.add(labelID, d);

		d.gridy = 1;
		fieldPanel.add(labelName, d);

		d.gridy = 2;
		fieldPanel.add(labelCourse, d);

		d.gridy = 3;
		fieldPanel.add(labelYear, d);

		d.gridy = 4;
		fieldPanel.add(labelClassCode, d);
		
		d.gridy = 5;
		fieldPanel.add(labelSchedule, d);

		d.gridx = 1;
		d.gridy = 0;
		fieldPanel.add(txtID, d);

		d.gridy = 1;
		fieldPanel.add(txtName, d);

		d.gridy = 2;
		fieldPanel.add(txtCourse, d);

		d.gridy = 3;
		fieldPanel.add(txtYear, d);

		d.gridy = 4;
		fieldPanel.add(txtClassCode, d);
		
		d.gridy = 5;
		fieldPanel.add(txtSchedule, d);

		//Declare JPanel for JButtons with GridLayout
		JPanel buttonPanel = new JPanel(new GridLayout(1,2));
		buttonPanel.add(buttonSave);
		buttonPanel.add(buttonExit);

		//Sets the JFrameLayout with GridBagLayout while using GridBagConstraints d
		setLayout(new GridBagLayout());
		d.gridx = 0;
		d.gridy = 0;
		d.weightx = 1.0;
		d.weighty = 1.0;
		d.insets = new Insets(5,5,5,5);
		add(fieldPanel, d);

		d.gridy = 1;
		add(buttonPanel, d);

		//Sets the variables of the JFrame of JDialog
		setVisible(true);
		setTitle(title);
		pack();

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e)
			{
				Window ButtonDialog = e.getWindow();
				ButtonDialog.dispose();
			}
		});

	}
	KeyListener keyListener = new KeyListener() {
		public void keyTyped(KeyEvent e) {
		}
		public void keyPressed(KeyEvent e) {
			keyCode = e.getKeyCode();
			
			if (!(keyCode >= 48 && keyCode <= 57) && !(keyCode >= 96 && keyCode <= 105)
				&& !(keyCode >= 37 && keyCode <= 40) && !(keyCode == 127 || keyCode == 8)) {
				txtID.setEditable(false);
				txtYear.setEditable(false);
            }
        }
        
        public void keyReleased(KeyEvent e) {
        	txtID.setEditable(true);
        	txtYear.setEditable(true);
        }
    };
    class JTextFieldLimit extends PlainDocument {
    	private int limit;

        JTextFieldLimit(int limit) {
        	super();
        	this.limit = limit;
        }
        
        JTextFieldLimit(int limit, boolean upper) {
        	super();
        	this.limit = limit;
        }
        public void insertString(int offset, String str, AttributeSet attr)
        	throws BadLocationException {
        	if (str == null)
        		return;
        		
        	if ((getLength() + str.length()) <= limit) {
        			super.insertString(offset, str, attr);
            }
        }
    }// end

	//Handles the action for closing JDialog
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == buttonExit){
			this.dispose();
		}

		else if(e.getSource() == buttonSave){
			if(edit){	//May be moved to a method and to StudentMainClass...
				int i = StudentGUI.tableStudList.getSelectedRow();
				StudentMainClass.updateTable(i);
				this.dispose();
			}
			else{
					if(txtID.getText().length() == 0 || txtName.getText().length() == 0 || txtCourse.getText().length() == 0 
						|| txtYear.getText().length() == 0 || txtClassCode.getText().length() == 0 || txtSchedule.getText().length() == 0){
						JOptionPane.showMessageDialog(null, "Please Fill All the Blanks.", "ERROR!",
                           JOptionPane.ERROR_MESSAGE);
					}
					else{
						addObject();
						this.dispose();
					}
			}		

		}

	}

	private void addObject(){ //May be moved to StudentMainClass
		StudentGUI.model.addRow(new Object[]{Integer.parseInt(txtID.getText()), txtName.getText(), txtCourse.getText(), 
			Integer.parseInt(txtYear.getText()), txtClassCode.getText().toString(), txtSchedule.getText().toString()});
	}

}
